package 华为机试8月16日机试;

import java.util.*;

public class 华为机试8月16日第二题自己写 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            //接受数据到list
            ArrayList<String> list = new ArrayList<>();
            while (true) {
                String line = sc.nextLine();
                if (line.charAt(line.length() - 1) != ',') {
                    list.add(simplify1(line));
                    break;
                }
                list.add(simplify1(line));
            }
            System.out.println("=====================================================");
            //查看原始的将接受数据list
            System.out.println("原始的list" + list);

            //把原始的list转化成array，输出查看
            String[] listArr = list2Arr(list);
            System.out.println("list2array" + Arrays.toString(listArr));

            //得到环形调用的部分,打印环形调用的部分
            String circles = getCirclePart(listArr);
            System.out.println("所有环形元素" + circles);
            System.out.println("=====================================================");
            //环形部分为false，原始的list输出
            printRes(circles, listArr);
            System.out.println("=====================================================");
        }
        sc.close();
    }

    private static void printRes(String circles, String[] listArr) {
        //不重复输出
        LinkedHashSet<String> hs_listArr = new LinkedHashSet<>();
        hs_listArr.addAll(Arrays.asList(listArr));
        String[] circles_split = circles.split("\\s+");
        //检查是不是环形调用的部分，输出
        for (String s : hs_listArr) {
            boolean isPrint = false;
            for (String aCircles_split : circles_split) {
                if (aCircles_split.equals(s)) {
                    System.out.println("{" + s + ",true" + "}");
                    isPrint = true;
                    break;
                }
            }
            if (!isPrint)
                System.out.println("{" + s + ",false" + "}");
        }

    }

    //得到环形调用的部分
    private static String getCirclePart(String[] listArr) {
        //得到序列里面的所有含有环形的链
        ArrayList<Stack<String>> allCircles = new ArrayList<>();
        for (int i = 0; i < listArr.length; i += 2) {
            Stack<String> stk = new Stack<>();
            stk.add(listArr[i]);
            stk.add(listArr[i + 1]);
            for (int j = i + 2; j < listArr.length; j += 2) {
                if (stk.peek().equals(listArr[j])) {
                    stk.add(listArr[j + 1]);
                }
            }
            if (stk.size() > 2)
                allCircles.add(stk);
        }
        //打印简化的所有含有环形的链
        System.out.println("所有含有环形的链" + allCircles.toString());

        //得到所有的环形，即得到环形调用的元素
        ArrayList<String> circles = new ArrayList<>();
        for (Stack<String> allLine : allCircles) {
            String stkStr = allLine.toString();
            String[] a = simplify2(stkStr).split("\\s+");
            if (a[0].equals(a[a.length - 1])) {
                System.out.println("每个环形" + simplify2(stkStr));
                circles.add(simplify2(stkStr));
            }
        }
        //返回环形
        return simplify2(circles.toString());
    }

    //list2array
    private static String[] list2Arr(ArrayList<String> list) {
        String[] res = new String[list.size() * 2];
        int j = 0;
        for (String aList : list) {
            String[] a = aList.split("\\s+");
            res[j++] = a[0];
            res[j++] = a[1];
        }
        return res;
    }

    private static String simplify2(String line) {
        return line.replaceAll("\\[", "").replaceAll("\\]", "").replaceAll("[,]", " ").trim();
    }

    private static String simplify1(String line) {
        return line.replaceAll("\\{", "").replaceAll("\\}", "").replaceAll("[,]", " ").trim();
    }

}
